package com.example.shiro2.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Post2 {
    public static String sendPost(String url, String content) {
        String result = "";
        try {
            //示例url
          //String url = "http://mon.rupeng119.com:8898/api/v1/ota/gateways/110011FF-0000-0001-00FF-4C3FD3305694/appconfig";
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "application/octet-stream");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            conn.setRequestProperty("Authorization", "Basic YWRtaW46cnVmZg==");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //发送文件
            //File file = new File("C:\\Users\\rp\\Desktop\\conf.json");
            //byte[] Bytes = new byte[(int)file.length()] ;
            // 发送请求参数
            DataOutputStream dos=new DataOutputStream(conn.getOutputStream());

//            InputStream os =  new FileInputStream(file);
           // os.read(Bytes);
            //System.out.println(Bytes[1]);
            //发送字符串
            dos.write(content.getBytes());

            // flush输出流的缓冲
            dos.flush();
            // 定义BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.out.println(result);//打印输出结果
        } catch (Exception e) {
            System.out.println("异常," + e.getMessage());
            result="异常," + e.getMessage();
            e.printStackTrace();
        }
        return result;
    }

    }
